package com.example.administrator.asdasd;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by Administrator on 2016/3/31.
 */
public class Login extends AppCompatActivity {
    private EditText lg_et1;
    private EditText lg_et2;
    private Button lg_btn1;
    private Button lg_btn2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        lg_btn1 = (Button) findViewById(R.id.loginButton);
        lg_btn2 = (Button) findViewById(R.id.regButton);
        lg_et1 = (EditText) findViewById(R.id.loginNum);
        lg_et2 = (EditText) findViewById(R.id.loginPassword);
        lg_btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLdm s = new SQLdm();
                SQLiteDatabase db1 = s.openDatabase(getApplicationContext());
                String str1 = lg_et1.getText().toString();
                Cursor c = db1.rawQuery("select * from grtb where name=?", new String[]{str1});
                String str =new String();
                while(c.moveToNext())
                {
                    str = c.getString(c.getColumnIndex("password"));
                }

                db1.close();

                if (str.equals(lg_et2.getText().toString())) {
                    Intent intent = new Intent(Login.this, MainActivity.class);
                    intent.putExtra("name",str1);
                    startActivity(intent);

                    finish();
                } else {
                    Toast.makeText(getApplicationContext(), "账号或密码错误", Toast.LENGTH_SHORT).show();
                }
            }
        });
        lg_btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SQLdm s = new SQLdm();
                SQLiteDatabase db1 = s.openDatabase(getApplicationContext());
                ContentValues val = new ContentValues();
                val.put("name", lg_et1.getText().toString());
                val.put("password", lg_et2.getText().toString());
                val.put("ren_name","");
                val.put("num", "");
                val.put("max", "");
                val.put("weizhi", "");
                db1.insert("grtb", null, val);
                val.clear();
                db1.close();
                Toast.makeText(getApplicationContext(), "注册成功，请登入", Toast.LENGTH_SHORT).show();
            }
        });

    }
}
